//
//  CNM_ExScreen.swift
//  CNMTools
//
//  Created by MrHong on 2023/12/6.
//

import UIKit

//MARK: - 安全区相关尺寸
public extension UIScreen {
    static var cnm_safeAreaWidth: CGFloat {
        guard let window: UIWindow = UIApplication.shared.windows.first else {
            return 0
        }
        if #available(iOS 11.0, *),
           UIWindow.instancesRespond(to: #selector(getter: window.safeAreaInsets)) {
            return cnm_screenWidth - window.safeAreaInsets.left - window.safeAreaInsets.right
        } else {
            return cnm_screenWidth
        }
    }
    
    static var cnm_safeAreaHeight: CGFloat {
        guard let window: UIWindow = UIApplication.shared.windows.first else {
            return 0
        }
        if #available(iOS 11.0, *),
           UIWindow.instancesRespond(to: #selector(getter: window.safeAreaInsets)) {
            return cnm_screenHeight - window.safeAreaInsets.bottom - window.safeAreaInsets.top
        } else {
            return cnm_screenHeight - UIApplication.shared.statusBarFrame.height
        }
    }
    
    static var cnm_safeAreaInsets: UIEdgeInsets {
        guard let window: UIWindow = cnm_curKeyWindow else {
            return .zero
        }
        if #available(iOS 11.0, *),
           UIWindow.instancesRespond(to: #selector(getter: window.safeAreaInsets)) {
            return window.safeAreaInsets
        }
        return .zero
    }
    
    static var cnm_safeAreaBottom: CGFloat {
        guard let window: UIWindow = cnm_curKeyWindow else {
            return 0
        }
        if #available(iOS 11.0, *),
           UIWindow.instancesRespond(to: #selector(getter: window.safeAreaInsets)) {
            return UIScreen.cnm_safeAreaInsets.bottom
        } else {
            return 0
        }
    }
    
    static var cnm_safeAreaTop: CGFloat {
        guard let window: UIWindow = cnm_curKeyWindow else {
            return 0
        }
        
        if #available(iOS 11.0, *),
           UIWindow.instancesRespond(to: #selector(getter: window.safeAreaInsets)) {
            return UIScreen.cnm_safeAreaInsets.top
        } else {
            return 0
        }
    }
    
    static var cnm_safeAreaLayoutGuide: UILayoutGuide {
        guard let window: UIWindow = cnm_curKeyWindow else {
            return UILayoutGuide()
        }
        
        if #available(iOS 11.0, *) {
            return window.safeAreaLayoutGuide
        }
        return UILayoutGuide()
    }
}
